#region imported Namespaces
using System;
using System.Data;
using System.Data.SqlClient;
using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
#endregion

namespace PHSRAG.Insight21.Purchasing
{
	#region Journal Entry Class
	/// <summary>
	/// Business class for the Journal Entry and Petty Cash.
	/// It encapsulates the methods to view, add, edit and delete
	/// journal entry and petty cash.
	/// </summary>
	public class JournalEntry: UserOrder
	{
		#region Constants

		//Stored Procedure Name
		private const string AddJEHeaderSPName		= "AddJEHeader";
		private const string UpdateJEHeaderSPName	= "UpdateJEHeader";
        private const string InvoiceNumberColumnName = "InvoiceNumber";
        private const string InvoiceDateColumnName  = "InvoiceDate";
		
		#endregion
		
		#region Constructor
		/// <summary>
		/// Initializes the base class with the specified cache policy
		/// </summary>
		/// <param name="cachePolicy">prevalent cache policy</param>
		public JournalEntry(ICachePolicy cachePolicy) : base(cachePolicy)
		{			
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Calls the virtual method in the base class to intialize the datacontainer and the
		/// data table for order header data and adds columns common to all user orders. In this
		/// method it adds column specific to journal entry to the table and adds an empty row in
		/// the table.
		/// </summary>
		public override void CreateHeaderTableAndAddRow()
		{
			base.CreateHeaderTableAndAddRow();
			DataTable orderHeader = DataContainer.Tables[Constants.UserOrderDetailsHeaderTableName];
            orderHeader.Columns.Add(InvoiceNumberColumnName, typeof(string));
            orderHeader.Columns.Add(InvoiceDateColumnName, typeof(string));
            orderHeader.Rows.Add(orderHeader.NewRow());
		}

		/// <summary>
		/// Intializes the connection object, creates command if its not already created and 
		/// calls the methods to add and update user order header. The operations add and 
		/// update is done through single interface because this class uses the data adapter
		/// and dataSet. The dataSet knows its RowState, based on which it decides to use the 
		/// add or update command objects to perform the appropriate operation. After the
		/// operation is complete the business cache of dependent reports are uncached.
		/// </summary>
		/// <param name="connectionString">open connection object</param>
		/// <param name="args">optional parameters for the method, as of now nothing 
		/// is expected for this argument</param>
		public override void Save(string connectionString, params object[] args )
		{
			SqlConnection connection = new SqlConnection(connectionString);			
			SqlTransaction transaction = null;
			try
			{
				connection.Open();
				LineItems.ValidateLineItems(connection);

				if(!isCommandCreated)
					CreateCommands();
				transaction = connection.BeginTransaction();
				ExecuteUpdate(transaction);
				LineItems.Save(transaction, OrderID);
				transaction.Commit();
				UserOrderHelper.UnCacheDependentReports(CachePolicy);
			}
			catch(Exception ex)
			{
				if(transaction != null)
                    transaction.Rollback();
				throw(new Exception(@"Error in updating user order.", ex));
			}
			finally
			{
				if (connection.State != ConnectionState.Closed)
					connection.Close();
			}
		}
		#endregion		

		#region Protected methods
		/// <summary>
		/// Creates the command objects for the add and update operation. 
		/// Calls the parent class method to initialize the command objects
		/// for add and update and add parameters to them. In this method, it
		/// sets the store procedures name specifc to journal entry  for add and 
		/// update commands and set the bool property isCommandCreated to true.
		/// </summary>
		protected override void CreateCommands()
		{
			base.CreateCommands();			
			addCommand.CommandText = AddJEHeaderSPName;
			updateCommand.CommandText = UpdateJEHeaderSPName;
			isCommandCreated = true;
		}
		#endregion
	}
	#endregion
}
